package com.rxt.thermopro.ui.dialog

import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.rxt.thermopro.R
import com.rxt.thermopro.app.AppContext
import com.rxt.thermopro.app.LocalDataDao
import java.util.*

class Temp100Dialog : DialogFragment() {

    private lateinit var contentView: View
    private var isShow = false
    private var mScreenDensity = 0f
    private var mScreenHeight = 0
    private var mScreenWidth = 0
    private val mHandler = Handler(Looper.getMainLooper())
    private lateinit var viewAlarmsText: TextView
    private lateinit var parentBg: ConstraintLayout


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireActivity())

        contentView = View.inflate(requireContext(), R.layout.dialog_temp_100, null)
        initListener()

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(contentView)
        dialog.setCanceledOnTouchOutside(false)

        val displayMetrics = DisplayMetrics()
        requireActivity().windowManager.defaultDisplay.getMetrics(displayMetrics)
        mScreenDensity = displayMetrics.density
        mScreenHeight = displayMetrics.heightPixels
        mScreenWidth = displayMetrics.widthPixels

        isCancelable = true
        val window: Window? = dialog.window
        if (window != null) {
            window.setBackgroundDrawable(ColorDrawable(resources.getColor(android.R.color.transparent)))
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            val lp: WindowManager.LayoutParams = window.attributes
            lp.gravity = Gravity.CENTER
            lp.width =  WindowManager.LayoutParams.MATCH_PARENT
            lp.height = WindowManager.LayoutParams.MATCH_PARENT
            window.attributes = lp
        }
        return dialog
    }

    private fun initListener() {
        viewAlarmsText = contentView.findViewById(R.id.viewAlarmsText)
        parentBg = contentView.findViewById(R.id.parentBg)
        viewAlarmsText.setOnClickListener {
             dismiss()
        }
        starting()
    }

    private val runnable= Runnable{
        if (parentBg.alpha==1f){
            parentBg.alpha=0.5f
        }else{
            parentBg.alpha=1f
        }
        mHandler.removeCallbacksAndMessages(null)
        starting()
    }

    private fun starting(){
        mHandler.postDelayed(runnable,400)
    }

    fun showDialog(fragmentManager: FragmentManager) {
        try {
            if (!isShow) {
                this.show(fragmentManager, "TAG")
                isShow = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("123->", "showDialog Exception $e")
        }
    }

    fun hideDialog() {
        if (isShow) {
            dismiss()
            isShow = false
        }
    }

    override fun dismiss() {
        mHandler.removeCallbacksAndMessages(null)
        super.dismiss()
    }

    override fun onResume() {
        super.onResume()
        isShow = true
    }

    override fun onPause() {
        super.onPause()
        isShow = false
    }

    override fun show(manager: FragmentManager, tag: String?) {
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }

}